package leetcode.day;

// 1974. 使用特殊打字机键入单词的最少时间
public class MinTimeToType {
    public static void main(String[] args) {
//        System.out.println('a'-'b');
        System.out.println(minTimeToType2("zjpc"));
    }

    public static int minTimeToType(String word) {
        int res = 0;
        for (int i = 0; i < word.length(); i++) {
            if (i == 0) {
                if (Math.abs(word.charAt(0) - 'a') >= 13) {
                    res += 26 - Math.abs(word.charAt(0) - 'a') + 1;
                } else {
                    res += Math.abs(word.charAt(0) - 'a') + 1;
                }
            } else {
                if (Math.abs(word.charAt(i) - word.charAt(i - 1)) >= 13) {
                    res += 26 - Math.abs(word.charAt(i) - word.charAt(i - 1)) + 1;
                } else {
                    res += Math.abs(word.charAt(i) - word.charAt(i - 1)) + 1;
                }
            }

        }
        return res;
    }

    // 模拟 || 单指针
    public static int minTimeToType2(String word) {
        char tmp = 'a';
        int res = 0;
        for (char c : word.toCharArray()) {
            int x = Math.abs(tmp - c);
            res +=Math.min(26-x,x)+1;
            tmp  = c; //指针后移
        }
        return res;
    }
}
